﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Support
{
    public class AllowedPublicPages
    {
        private static Dictionary<string, string> _publicPages = null;

        public static Dictionary<string, string> PublicPages
        {
            get 
            {
                if (_publicPages == null)
                    _publicPages = getPublicPages();
                return _publicPages;
            }
        }

        private static Dictionary<string, string> getPublicPages()
        {

            Dictionary<string, string> publicPages = new Dictionary<string, string>();

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        SPList publicPagesList = currentWeb.Lists[GlobalConstants.LIST_PUBLIC_PAGES];
                        publicPages = (from item in publicPagesList.Items.OfType<SPListItem>()
                                       orderby item.Title descending
                                       select item).ToDictionary(c => c.Title, c => c.Title);
                    }
                }
            });

            return publicPages;
        }

        public static bool IsPagePublicPage(string url)
        {
            if (PublicPages.ContainsKey(url.ToUpper()))
                return true;
            return false;
        }

        private AllowedPublicPages()
        { }

    }
}
